/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Dao;

import Pojo.Sach;
import Util.MySqlDataAccessHelper;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author USER
 */
public class SachDAO {

    public static List<Sach> laySach(String mySql) {
        List<Sach> kq = new ArrayList<Sach>();
        try {
            MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
            helper.open();
            ResultSet rs = helper.executeQuery(mySql);
            while (rs.next()) {
                Sach s = new Sach();
                s.setMaSach(rs.getInt("ma_sach"));
                s.setTenSach(rs.getString("ten_sach"));
                s.setTacGia(rs.getString("tac_gia"));
                s.setHinhBia(rs.getString("hinh_bia"));
                s.setHinhChiTiet(rs.getString("hinh_chi_tiet"));
                s.setMaNhaXuatBan(rs.getInt("ma_nha_xuat_ban"));
                s.setMaTheLoai(rs.getInt("ma_the_loai"));
                s.setMaLoaiSach(rs.getInt("ma_loai_sach"));
                s.setGiaBan(rs.getFloat("gia_ban"));
                s.setSoLuongBan(rs.getInt("so_luong_ban"));
                s.setSoLuongTon(rs.getInt("so_luong_ton"));
                s.setTinhTrang(rs.getInt("tinh_trang"));
                s.setNgayNhap(rs.getDate("ngay_nhap").toString());
                kq.add(s);
            }
            helper.close();
        } catch (Exception e) {
        }
        return kq;
    }
    
    public static boolean themSach(Sach s){
        boolean kq;
        MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
        helper.open();
        String sql = String.format("INSERT INTO `sach` (`ma_sach`,`ten_sach`,`tac_gia`,`so_luong_ton`,`so_luong_ban`,`tinh_trang`,`hinh_bia`,`hinh_chi_tiet`,`ngay_nhap`,`gia_ban`,`ma_the_loai`,`ma_nha_xuat_ban`,`ma_loai_sach`) VALUES(%d, '%s', '%s', %d, %d, %d, '%s', '%s','%s', %f, %d, %d, %d)", 
                s.getMaSach(), s.getTenSach(), s.getTacGia(), s.getSoLuongTon(), s.getSoLuongBan(),
                s.getTinhTrang(), s.getHinhBia(), s.getHinhChiTiet(), s.getNgayNhap() ,s.getGiaBan(),
                s.getMaTheLoai(), s.getMaNhaXuatBan(), s.getMaLoaiSach());
        kq = helper.executeNonQuery(sql);
        helper.close();
        return kq;
       
    }
    
     public static boolean capNhatSach(Sach s){
        boolean kq;
        MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
        helper.open();
        String sql = String.format("UPDATE `sach` SET `ten_sach`='%s',`tac_gia`='%s',`so_luong_ton`=%d,`so_luong_ban`=%d,`tinh_trang`=%d,`hinh_bia`='%s',`hinh_chi_tiet`='%s',`ngay_nhap`='%s',`gia_ban`=%f,`ma_the_loai`=%d,`ma_nha_xuat_ban`=%d WHERE `ma_sach`=%d",
                s.getTenSach(), s.getTacGia(), s.getSoLuongTon(), s.getSoLuongBan(),
                s.getTinhTrang(), s.getHinhBia(), s.getHinhChiTiet(), s.getNgayNhap() ,s.getGiaBan(),
                s.getMaTheLoai(), s.getMaNhaXuatBan(),s.getMaSach());
        kq = helper.executeNonQuery(sql);
        helper.close();
        return kq;
       
    }

   
}
